using System;
using System.Collections.Generic;
using System.Reflection;
using InfiniTec.Data;

namespace InfiniTec.DirectoryServices
{
    internal static class SchemaMapping
    {
        public static readonly IPropertyDefinition[] CommonSchema;
        public static readonly IPropertyDefinition[] GroupSchema;
        public static readonly IPropertyDefinition[] PrincipalSchema;
        public static readonly IPropertyDefinition[] UserSchema;

        static SchemaMapping()
        {
            CommonSchema = ExtractFieldValues(typeof (WellknownProperties.Common));
            PrincipalSchema = ExtractFieldValues(typeof (WellknownProperties.GroupMembership));
            UserSchema = ExtractFieldValues(typeof (WellknownProperties.User));
            GroupSchema = ExtractFieldValues(typeof (WellknownProperties.Group));
        }

        private static IPropertyDefinition[] ExtractFieldValues(Type t)
        {
            Type propertyDefinitionType = typeof (IPropertyDefinition);

            var result = new List<IPropertyDefinition>();
            foreach (FieldInfo info in t.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static))
            {
                if (!propertyDefinitionType.IsAssignableFrom(info.FieldType)) continue;

                result.Add((IPropertyDefinition) info.GetValue(null));
            }

            return result.ToArray();
        }
    }
}